package com.gxa.weixing.portal.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Address;
import com.gxa.weixing.core.pojo.GetGoodsFromCartVO;
import com.gxa.weixing.core.pojo.Order;
import com.gxa.weixing.core.pojo.OrderGoodsVO;
import com.gxa.weixing.core.util.JsonUtil;
import com.gxa.weixing.portal.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author HHT
 * 2022/1/6
 */
@RestController
@RequestMapping("/order")
@Api(value = "orderController",tags = "顶单的接口")
public class OrderController {


    @Autowired
    OrderService orderService;

    @GetMapping("/orderList")
    @ApiOperation("订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "long",required = true)
    })
    public Map getOrderList(Long id){
        List<Map> list=new ArrayList<>();
        Map map=new HashMap();
        map.put("img","www.fff");
        map.put("desc","这是什么");
        map.put("num","2");
        map.put("price","5");
        map.put("prices","10");
        map.put("coupon","3");
        list.add(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/settlement")
    @ApiOperation("结算")
    public Map settlement(@RequestBody Map map){
        System.out.println(map);
        List list =(List) map.get("list");
        System.out.println(list);
        Double price =Double.valueOf(map.get("price").toString()) ;
        System.out.println(price);
        Long userId = Long.parseLong(map.get("userId").toString());
        List<GetGoodsFromCartVO> settlement = orderService.settlement(list, price, userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(settlement);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.put("price",price);
        return jsonResult.getDataMap();
    }

    @PostMapping("/SubmitOrder")
    @ApiOperation("提交订单")

    public Map SubmitOrder(@RequestBody Order order, HttpServletRequest request, HttpServletResponse response) throws BusinessException, ServletException, IOException {
        Order order1 = orderService.SubmitOrder(order);
        request.setAttribute("order",order1);
        request.setAttribute("price",order1.getPayPrice());
        request.setAttribute("str",order1.getOrderNo());
        request.getServletContext().getRequestDispatcher("/pay").forward(request,response);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/zhuanfa")
    public void zhuanfa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("str","54546546545456");
        request.setAttribute("money","45546");
        request.getServletContext().getRequestDispatcher("/pay").forward(request,response);

    }
}
